<template>
  <div style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 500px; overflow-y: hidden;"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>
<script>
import Vue from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {  getNews } from '@/api/system/news'
import { getBanner } from '../../api/system/banner'
import { getToken } from '@/utils/auth'

export default Vue.extend({
  components: { Editor, Toolbar },
  props: {
    newsContent: String
  },
  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {},
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF:{

        }
      },
      mode: 'default' // or 'simple'
    }
  },
  created() {
    this.editorConfig.MENU_CONF['uploadImage'] = {
      server: process.env.VUE_APP_BASE_API +'/system/file/wangeditor',
      fieldName: 'file',
      // 自定义增加 Authorization  header
      headers: {
        'Authorization': "Bearer " + getToken(),
      },
      customInsert(res, insertFn) {

        // res 即服务端的返回结果
        // 从 res 中找到 url alt href ，然后插图图片
        insertFn(res.data.filePath, res.data.fileName, res.data.fileName)
      },
    }
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    setHtml(){
      this.editor.setHtml(this.newsContent)
    },
    getHtml(){
      return this.editor.getHtml();
    },
    //拉去新闻内容
    initEditor(newsId){
      getNews(newsId).then(response => {
        this.html = response.data.newsContent
      })
    },
    //拉取轮播内容
    initEditor2(id){
      getBanner(id).then(response => {
        console.log(response,"responseresponseresponse")
        this.html = response.data.bannerContent
      })
    },
  },
  mounted() {

  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
})
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
